﻿#region Usings

using System;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

#endregion

namespace ScrumTable.UI.View.CustomControl
{
    /// <summary>
    /// digital clock
    /// </summary>
    public class DigitalClock : Label
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        private DateTime _time;
        private Timer _myDispatcherTimer;
        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="DigitalClock"/> class.
        /// </summary>
        public DigitalClock()
        {
            Content = "00:00:00";
            _myDispatcherTimer  = new System.Timers.Timer(1000); //1sek
            _myDispatcherTimer.Elapsed += new ElapsedEventHandler(Clock);
            _myDispatcherTimer.Start();
        }

           /// <summary>
        /// Dispose instance manually.
        /// </summary>
        ~DigitalClock()
        {
            Dispose(false);
        }

        #endregion


          /// <summary>
        /// Implement dispose interface in order to support runtime support.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);

            // Use SupressFinalize in case a subclass
            // of this type implements a finalizer.
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes the managed and unmanaged resources.
        /// </summary>
        /// <param name="disposing">True to dispose the managed resources, otherwise false.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                _myDispatcherTimer.Stop();
                _myDispatcherTimer.Elapsed -= new ElapsedEventHandler(Clock);
            }
        }





        #region Methods

        /// <summary>
        /// Resets the timer.
        /// </summary>
        public void Reset()
        {
            _time = new DateTime();
            Content = "00:00:00";
        }
 


        // Fires every 1 seconds while the DispatcherTimer is active.
        private void Clock(object o, EventArgs sender)
        {
            Dispatcher.Invoke((Action)(() =>
                                           {
                                               _time = _time.AddSeconds(1);
                                               Content = String.Format("{0:00}", _time.Hour) + ":" +
                                                         String.Format("{0:00}", _time.Minute) + ":" +
                                                         String.Format("{0:00}", _time.Second);
                                           }

                                      ));
        }
        #endregion
    }
}
